#!/usr/bin/env bash
. ./scripts/devcontainer/_assert-in-container "$0" "$@"

set -euo pipefail

PUBLISHED=false
FLAGS=""
NO_LOG=false

for i in "$@"
do
  case "${i}" in
    --published) shift; PUBLISHED=true ;;
    --flags=*) FLAGS="${i#*=}" ;;
    --no-log) shift; NO_LOG=true ;;
  esac
done

if [[ -n "$FLAGS" ]]; then
  echo "Flags: $FLAGS"
fi

LOGS="${DARK_CONFIG_RUNDIR}/logs"
CLI_LOG="$LOGS/cli.log"
PACKAGES_LOG="$LOGS/packages-canvas.log"

if [[ "$PUBLISHED" == "true" ]]; then
  EXE="backend/Build/out/Cli/Release/net8.0/linux-x64/publish/Cli"
else
  EXE="backend/Build/out/Cli/Debug/net8.0/Cli"
fi

# if it hasn't been compiled yet, wait for it

for ((i=1;i<=1000;i++));
do
  if [[ ! -f "${EXE}" ]]; then
    echo "Waiting for compiled binary"
    sleep 0.01
  fi
done

# Check if packages have loaded successfully
if [[ -f "${PACKAGES_LOG}" ]]; then
  # Check if the log ends with an exception/error (indicating failed package loading)
  if tail -n 10 "${PACKAGES_LOG}" | grep -q "Exception\|Shutting down LibService"; then
    echo "ERROR: Packages failed to load! Check ${PACKAGES_LOG} for details."
    echo "You must wait for packages to finish loading successfully before running the CLI."
    echo "Recent log entries:"
    tail -n 20 "${PACKAGES_LOG}"
    exit 1
  fi

  # Check if packages are still loading
  if tail -n 5 "${PACKAGES_LOG}" | grep -q "DEBUG: about to parse"; then
    echo "ERROR: Packages are still loading! Wait for package loading to complete."
    echo "Check ${PACKAGES_LOG} and wait for 'Starting: reading, parsing packages' to complete successfully."
    echo "Recent log entries:"
    tail -n 10 "${PACKAGES_LOG}"
    exit 1
  fi
fi

GIT_COMMIT=$(git rev-parse --short HEAD)
export GIT_COMMIT

# context: 'tee' was screwing up our MCP server running through this script
# so a --no-log argument is supported to skip logging to the file
if [[ "$NO_LOG" == "true" ]]; then
  "${EXE}" "$@"
else
  "${EXE}" "$@" 2>&1 | tee "${CLI_LOG}"
fi

